const msgWrapper = require("../lib/utilx").msgWrapper;
const generateLoginToken = require("../lib/utilx").generatorLoginToken;
const Model = require("../models/index").models;
const Admin = Model.Admin;
const auth  = require("../instances/auth");


module.exports = (router) => {
    router.post("/login", function*(){
        var ctx = this;
        ctx.checkBody("account").notEmpty();
        ctx.checkBody("password").notEmpty();

        var account = ctx.request.body.account;
        var password = ctx.request.body.password;
        var adminer = yield Admin.find({
            where : {
                account,
                password
            }
        });
        if(adminer){
            var loginToken = generateLoginToken(account);
            yield auth.login(ctx,loginToken,adminer);
            ctx.redirect("/token/home");
        }else{
            ctx.redirect("/?errorCode=1");
        }
    });
};